import request from "../request"
import EventEmitter from "../src/common/EventEmitter.js"
// import RCTDeviceEventEmitter from 'RCTDeviceEventEmitter'

export const CollectionChangedEvent = "collection_changed"

export let List = async function () {
    let json = await request({
        url: '/api/collection/',
        type: "GET"
    })
    return json.result
}

export let Get = async function (id) {
    let json = await request({
        url: '/api/collection/' + id,
        type: "GET"
    })
    return json.result
}

export let Add = async function ({name, content}) {
    let json = await request({
        url: '/api/collection/',
        type: 'POST',
        params: {
            name: name,
            content: content
        }
    })
    EventEmitter.emit(CollectionChangedEvent, json.accessToken)
    //todo: RCTDeviceEventEmitter.emit(CollectionChangedEvent, json.accessToken)
    return json.result
}

// export let UpdateSort = async function (collections: []) {
//     // 更新排序,保证数组本身有序
//     //collections:{id}
//     let json = await request({
//         url: '/api/collection/multi/',
//         type: 'PUT',
//         params: collections
//     })
//     RCTDeviceEventEmitter.emit(CollectionChangedEvent, json.accessToken)
//     return json.result
// }

export let Update = async function ({id, name, content}) {
    let json = await request({
        url: '/api/collection/' + id,
        type: 'PUT',
        params: {
            name: name,
            content: content
        }
    })
    EventEmitter.emit(CollectionChangedEvent, json.accessToken)
    //todo: RCTDeviceEventEmitter.emit(CollectionChangedEvent, json.accessToken)
    return json.result
}

export let Delete = async function (id) {
    let json = await request({
        url: '/api/collection/' + id,
        type: 'DELETE'
    })
    EventEmitter.emit(CollectionChangedEvent, json.accessToken)
    //todo: RCTDeviceEventEmitter.emit(CollectionChangedEvent, json.accessToken)
    return json.result
}
